<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 15-11-17
 * Time: 下午6:54
 */
include '/var/www/html/new/com.inc.php';
$ret = Get_ScenicSpot_List(200248,'');
var_dump($ret);
//景区列表
function Get_ScenicSpot_List($ac, $pw, $n=NULL, $m=NULL) {
//        $this->logrec("Get_ScenicSpot_List#ac:$ac, pw:$pw, n:$n, m:$m");
        $str="select id from pft_member where account='$ac' and status=0";
        $GLOBALS['le']->query($str);
        if (!$GLOBALS['le']->fetch_assoc()) return 1;
        $fxs_id=$GLOBALS['le']->f('id');
        $aland=array();
        //自供自销
        $str="select f.lid,t.apply_did as aid from uu_jq_ticket as t left join uu_land_f as f on t.id=f.tid left join uu_products p on p.id=t.pid where p.p_status=0 and p.apply_limit=1 and t.apply_did=$fxs_id group by f.lid";
        $GLOBALS['le']->query($str);
        while ($GLOBALS['le']->fetch_assoc()) {
            $aland[]=$GLOBALS['le']->f('lid').','.$GLOBALS['le']->f('aid');
        }
        echo '111';
        //$alandstr=implode(',',$aland);

        //直销可购买的产品
        $str="select pids,aid from pft_product_sale_list where fid=$fxs_id and status=0";
        $GLOBALS['le']->query($str);
        $a_pids=array();
        while ($row=$GLOBALS['le']->fetch_assoc()) {
            $a_pids[]=$row;
        }
//        print_r($a_pids);
//        echo '22212';
        foreach ($a_pids as $k=>$v){
            if ($v['pids']=='A'){
                $str="select f.lid,t.apply_did as aid from uu_jq_ticket as t left join uu_land_f as f on t.id=f.tid left join uu_products p on p.id=t.pid where p.p_status=0 and p.apply_limit=1 and t.apply_did=".$v['aid']." group by f.lid";
                $GLOBALS['le']->query($str);
                while ($GLOBALS['le']->fetch_assoc()) {
                    $aland[]=$GLOBALS['le']->f('lid').','.$GLOBALS['le']->f('aid');
                }
            }elseif($v['pids']){
                $str="select f.lid,t.apply_did as aid from uu_jq_ticket as t left join uu_land_f as f on t.id=f.tid left join uu_products p on p.id=t.pid where p.p_status=0 and p.apply_limit=1 and t.pid in (".$v['pids'].") group by f.lid";
                echo $v['aid'],':',$str . "\n";

                $GLOBALS['le']->query($str);

                while ($GLOBALS['le']->fetch_assoc()) {
                    $aland[]=$GLOBALS['le']->f('lid').','.$GLOBALS['le']->f('aid');
                }
            }
        }
        echo '222';
        //转分销可购买的产品
        $str="select pid,sid from pft_p_apply_evolute where fid='$fxs_id' and status=0";
        $GLOBALS['le']->query($str);
        $evo_pids=array();
        while ($row=$GLOBALS['le']->fetch_assoc()) {
            $evo_pids[]=$row;
        }

        foreach ($evo_pids as $k=>$v){
            $str="select f.lid,t.apply_did as aid from uu_jq_ticket as t left join uu_land_f as f on t.id=f.tid left join uu_products p on p.id=t.pid where p.p_status=0 and p.apply_limit=1 and t.pid=".$v['pid']." group by f.lid";
            $GLOBALS['le']->query($str);
            while ($GLOBALS['le']->fetch_assoc()) {
                $aland[]=$GLOBALS['le']->f('lid').','.$v['sid'];
            }
        }
        //剔除重复的
        $alandU=array_unique($aland);
        if (!$alandU) return 2;

        $a1=array();
        $arrL=array();
        $arrA=array();
        foreach ($alandU as $val) {
            $arrL[]=current(explode(',',$val));
            $arrA[]=end(explode(',',$val));
        }
        $alandstr=implode(',',$arrL);
        $str="select id,title,addtime,imgpath,area,p_type from uu_land where id in ($alandstr) order by px desc";
        if ($n) $str.=" limit ".$n;
        if ($m && $n) $str.=",$m";
        $GLOBALS['le']->query($str);
        while ($row=$GLOBALS['le']->fetch_assoc()) {
            $a1[]=$row;
        }
        //return $str;
        if (count($a1)<1) {
            return 3;
        }else{
            foreach ($a1 as $key=>$val){
                $kword=$val[area];
                $akword=explode("|",$kword);
                $apword=array();

                $str="select area_name from uu_area where area_id='".$akword[0]."' or area_id='".$akword[1]."' or area_id='".$akword[2]."'";
                $GLOBALS['le']->query($str);
                while ($GLOBALS['le']->fetch_assoc()) {
                    $apword[]=$GLOBALS['le']->f('area_name');
                }
                //return implode('|',$apword);
                $a1[$key]['area']=implode('|',$apword);
            }
            return$a1;
        }
}